<template>
  <el-dialog :title="form.id ? $t('common.editBtn') : $t('common.addBtn')" v-model="visible" :close-on-click-modal="false"
    draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="12" class="mb20">
          <el-form-item prop="name" :label="$t('ProdGroup.name')">
            <el-input v-model="form.name" :placeholder="$t('ProdGroup.inputNameTip')" clearable show-word-limit
              :maxlength="32" />
          </el-form-item>
        </el-col>

        <!-- <el-col :span="12" class="mb20">
          <el-form-item prop="shopId" :label="$t('ProdGroup.shopId')">
            <ShopSelect v-model="form.shopId"></ShopSelect>
          </el-form-item>
        </el-col> -->

        <el-col :span="12" class="mb20">
          <el-form-item prop="state" :label="$t('ProdGroup.state')">
            <el-switch v-model="form.state" :active-value="1" :inactive-value="0"></el-switch>
          </el-form-item>
        </el-col>

        <!-- <el-col :span="12" class="mb20">
          <el-form-item prop="isDefault" :label="$t('ProdGroup.isDefault')">
            <el-select v-model="form.isDefault">
              <el-option :value="false" :label="$t('ProdGroup.default')"></el-option>
              <el-option :value="true" :label="$t('ProdGroup.default')">商家自定义</el-option>
            </el-select>

            <el-input v-model="form.isDefault" :placeholder="$t('ProdGroup.inputIsDefaultTip')" clearable
              show-word-limit />
          </el-form-item>
        </el-col> -->

        <el-col :span="12" class="mb20">
          <el-form-item prop="prodCount" :label="$t('ProdGroup.prodCount')">
            <el-input-number min="1" v-model="form.prodCount" :placeholder="$t('ProdGroup.inputProdCountTip')" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="style" :label="$t('ProdGroup.style')">
            <el-select v-model="form.style">
              <el-option :value="1" :label="$t('ProdGroup.oneInAColumn')"></el-option>
              <el-option :value="2" :label="$t('ProdGroup.twoInAColumn')"></el-option>
              <el-option :value="3" :label="$t('ProdGroup.triad')"></el-option>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="12" class="mb20">
          <el-form-item prop="sort" :label="$t('ProdGroup.sort')">
            <el-input-number min="0" v-model="form.sort" :placeholder="$t('ProdGroup.inputSortTip')" clearable
              show-word-limit />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">{{ $t('common.cancelButtonText') }}</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">{{ $t('common.confirmButtonText') }}</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="ProdGroupDialog">
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/bizs/ProdGroup'
const emit = defineEmits(['refresh']);

// 定义i18n
import { useI18n } from 'vue-i18n';
const { t } = useI18n();

// 定义变量内容
const visible = ref(false)
const loading = ref(false)
const dataFormRef = ref();

// 提交表单数据
const form = reactive({
  id: '',
  name: '',
  shopId: '',
  state: '',
  isDefault: '',
  prodCount: '',
  style: '',
  sort: '',
});

// 定义校验规则
// import { rule } from '/@/utils/validate';
const dataRules = ref({
  name: [{ required: true, message: t('ProdGroup.inputNameTip'), trigger: 'blur' }],
  shopId: [{ required: false, message: t('ProdGroup.inputShopIdTip'), trigger: 'blur' }],
  state: [{ required: false, message: t('ProdGroup.inputStateTip'), trigger: 'blur' }],
  isDefault: [{ required: false, message: t('ProdGroup.inputIsDefaultTip'), trigger: 'blur' }],
  prodCount: [{ required: true, message: t('ProdGroup.inputProdCountTip'), trigger: 'blur' }],
  style: [{ required: true, message: t('ProdGroup.inputStyleTip'), trigger: 'blur' }],
  sort: [{ required: false, message: t('ProdGroup.inputSortTip'), trigger: 'blur' }],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取信息
  if (id) {
    form.id = id
    initFormData(id)
  }
};

// 初始化表单
const initFormData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 提交表单
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? t('common.editSuccessText') : t('common.addSuccessText'));
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 暴露变量
defineExpose({
  openDialog
});
</script>
